#include "StdAfx.h"
#include "ExListViewCtrl.h"

const CRect CExListViewCtrl::ICON_INSETS(2, 2, 2, 2);
const CRect CExListViewCtrl::TEXT_INSETS(0, 0, 2, 0);

CExListViewCtrl::CExListViewCtrl()
{
}

void CExListViewCtrl::Attach(HWND hWndNew) throw()
{
    BOOL r = SubclassWindow(hWndNew);
    if (r == TRUE)
    {
        ModifyStyle(0, LVS_OWNERDRAWFIXED, 0);
    }
}

HWND CExListViewCtrl::Detach() throw()
{
    return UnsubclassWindow();
}

void CExListViewCtrl::DrawItem(LPDRAWITEMSTRUCT lpDrawItemStruct)
{
    if (lpDrawItemStruct == NULL)
    {
        ATLTRACE(_T("CExListViewCtrl::DrawItem - lpDrawItemStruct NULL\n"));
        return;
    }

    if ((lpDrawItemStruct->itemState & ODS_SELECTED) > 0)
    {
        FillRect(
            lpDrawItemStruct->hDC,
            &lpDrawItemStruct->rcItem,
            GetSysColorBrush(COLOR_HIGHLIGHT));

        ::SetTextColor(
            lpDrawItemStruct->hDC,
            GetSysColor(COLOR_HIGHLIGHTTEXT));
    }
    else
    {
        FillRect(
            lpDrawItemStruct->hDC,
            &lpDrawItemStruct->rcItem,
            GetSysColorBrush(COLOR_WINDOW));

        ::SetTextColor(
            lpDrawItemStruct->hDC,
            GetSysColor(COLOR_WINDOWTEXT));
    }

    int ColCount = GetHeader().GetItemCount();
    for (int i = 0; i < ColCount; i++)
    {
        CString Text;

        LVITEM Item = {0};
        Item.mask = LVIF_IMAGE | LVIF_TEXT;
        Item.iItem = lpDrawItemStruct->itemID;
        Item.iSubItem = i;
        Item.pszText = Text.GetBuffer(MAX_PATH);
        Item.cchTextMax = MAX_PATH;
        BOOL r = GetItem(&Item);
        Text.ReleaseBuffer();
        if (r == FALSE)
        {
            ATLTRACE(_T("CExListViewCtrl::DrawItem - GetItem failed\n"));
            return;
        }

        CRect TextRect;
        GetSubItemRect(Item.iItem, Item.iSubItem, LVIR_LABEL, &TextRect);
        TextRect.DeflateRect(TEXT_INSETS);

        if (Item.iSubItem == 0)
        {
            CImageList ImageList = GetImageList(LVSIL_SMALL);
            CIcon Icon = ImageList.GetIcon(Item.iImage);
            (void)Icon.DrawIcon(
                lpDrawItemStruct->hDC,
                lpDrawItemStruct->rcItem.left + ICON_INSETS.left,
                lpDrawItemStruct->rcItem.top + ICON_INSETS.top);

            // Adjust text rectangle to account for insets
            int TextLeftOffset = ICON_INSETS.left + ICON_INSETS.right;
            TextRect.left += TextLeftOffset;
            TextRect.right -= TextLeftOffset;
        }

        LVCOLUMN Col = {0};
        Col.mask = LVCF_FMT;
        r = GetColumn(Item.iSubItem, &Col);
        if (r == FALSE)
        {
            ATLTRACE(_T("CExListViewCtrl::DrawItem - GetColumn failed\n"));
            return;
        }

        UINT DTFlags = DT_SINGLELINE | DT_VCENTER | DT_END_ELLIPSIS |
            DT_EDITCONTROL;
        if ((Col.fmt & LVCFMT_LEFT) > 0)
            DTFlags |= DT_LEFT;
        else if ((Col.fmt & LVCFMT_RIGHT) > 0)
            DTFlags |= DT_RIGHT;

        DrawText(
            lpDrawItemStruct->hDC,
            Text,
            Text.GetLength(),
            &TextRect,
            DTFlags);
    }
}

void CExListViewCtrl::MeasureItem(LPMEASUREITEMSTRUCT lpMeasureItemStruct)
{
    if (lpMeasureItemStruct == NULL)
    {
        ATLTRACE(_T("CExListViewCtrl::DrawItem - lpDrawItemStruct NULL\n"));
        return;
    }

    CImageList ImageList = GetImageList(LVSIL_SMALL);
    int w = 0;
    int h = 0;
    ImageList.GetIconSize(w, h);
    UNREFERENCED_PARAMETER(w);
    lpMeasureItemStruct->itemHeight =
        (UINT)(h + ICON_INSETS.top + ICON_INSETS.bottom);
}
